<?php
namespace Cyfy\Modules\PasswordRecovery\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\User;

class Recovery extends Forms\Form
{
    private $uids = array();
    
	function structure()
	{		
		$this -> stay = false;
        
        $i = new Forms\TextBox("usernameEmail", self :: t(10));
        $i -> maxlength = 255;
        $i -> required = true;
        $i -> description = self :: t(20);
        $this -> addItem($i);
    
		$b = new Forms\Button(self :: t(30));
		$this -> addButton($b);
    }
    
    function validate()
    {
		$result = \dibi :: query("SELECT [uid] FROM [:cyfy:Users-users] WHERE [email] = %s", $this -> get("usernameEmail"), " OR username = %s", $this -> get("usernameEmail"));
        if(count($result) == 0)
        {
            \Cyfy\Message :: set(self :: t(40), \Cyfy\Message :: ERROR);
            return false;
        }
        // user/users found
        foreach($result as $u)
        {
            $this -> uids[] = $u["uid"];
        }
        return true;
    }

    
	function submit()
	{  
        foreach($this -> uids as $uid)
        {
            $status = \Cyfy\Modules\PasswordRecovery\Module :: recoverForUser($uid);        
            if($status)
            {
                \Cyfy\Message :: set(self :: t(50));
            }
            else
            {
                \Cyfy\Message :: set(self :: t(60), \Cyfy\Message::ERROR);
            }
        }
        return true;
	}	

}
